package com.dk.shiro;

import com.alibaba.fastjson.JSONObject;
import com.dk.api.UserServiceApI;
import com.dk.dto.UserDTO;
import com.dk.em.Result;
import com.dk.pojo.User;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

public class LoginRealm extends AuthorizingRealm {

    @Autowired
    private UserServiceApI userServiceApI;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        UsernamePasswordToken usernameToken=(UsernamePasswordToken) authenticationToken;
        Result<UserDTO> userDTOResult = userServiceApI.LogingetUserByName(usernameToken.getUsername());
        UserDTO dto = userDTOResult.getData();
        if (dto == null){
            return null;
        }
        SimpleAuthenticationInfo loginRealm = new SimpleAuthenticationInfo(dto, dto.getUPwd(), ByteSource.Util.bytes(dto.getUSalt()), "LoginRealm");
        return loginRealm;
    }
}
